//
//  GGTools.m
//  unzip
//
//  Created by yg on 2021/11/15.
//

#import "GGTools.h"

@implementation GGTools

//actionSheet
+ (void)showActionSheetWithTitle:(NSString *)title message:(NSString *)message items:(NSArray *)items style:(UIAlertControllerStyle)style atController:(UIViewController *)controller action:(void(^)(NSUInteger itemIndex))itemAction
{
	UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:style];
	UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
	[actionSheetController addAction:cancelAction];
	[items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
		UIAlertAction *sheetAction = [UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
			itemAction(idx);
		}];
		[actionSheetController addAction:sheetAction];
	}];
	[controller presentViewController:actionSheetController animated:YES completion:nil];
}

//actionSheet
+ (void)showTextFieldActionWithTitle:(NSString *)title message:(NSString *)message placeholder:(NSString *)placeholder items:(NSArray *)items atController:(UIViewController *)controller action:(void(^)(NSUInteger itemIndex,UITextField *textField))itemAction
{
	UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
	UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
	[actionSheetController addAction:cancelAction];
	
	[actionSheetController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
		textField.placeholder = placeholder;
		textField.returnKeyType = UIReturnKeyDone;
	}];
	
	__weak UIAlertController *weakActionSheetController = actionSheetController;
	[items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
		UIAlertAction *sheetAction = [UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
			itemAction(idx,weakActionSheetController.textFields.firstObject);
		}];
		[actionSheetController addAction:sheetAction];
	}];
	[controller presentViewController:actionSheetController animated:YES completion:nil];
}

+ (void)showToast:(NSString *)toast{
	NSLog(@"%@", toast);
	if (toast.length == 0) {
		return;
	}
	
	UIWindow *mainWindow = [[UIApplication sharedApplication].delegate window];
	
	UILabel *label = [[UILabel  alloc] init];
	label.text = toast;
	label.textColor = [UIColor whiteColor];
	label.textAlignment = NSTextAlignmentCenter;
	label.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
	[mainWindow addSubview:label];
	CGSize size = [label sizeThatFits:CGSizeZero];
	label.frame = CGRectMake(0, 0, size.width + 30, size.height + 10);
	label.center = CGPointMake(mainWindow.frame.size.width / 2, mainWindow.frame.size.height / 2);
	label.alpha = 0;
	[UIView animateWithDuration:0.1 animations:^{
		label.alpha = 1;
	} completion:^(BOOL finished) {
		[UIView animateWithDuration:3 animations:^{
			label.alpha = 0.99;
		} completion:^(BOOL finished) {
			[UIView animateWithDuration:0.1 animations:^{
				label.alpha = 0;
			} completion:^(BOOL finished) {
				[label removeFromSuperview];
			}];
		}];
	}];
}

+ (BOOL)getProxyStatus {
	NSDictionary *proxySettings =  (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());
	NSArray *proxies = (__bridge NSArray *)(CFNetworkCopyProxiesForURL((__bridge CFURLRef _Nonnull)([NSURL URLWithString:@"http://www.baidu.com"]), (__bridge CFDictionaryRef _Nonnull)(proxySettings)));
	NSDictionary *settings = [proxies objectAtIndex:0];
	
	NSLog(@"host=%@", [settings objectForKey:(NSString *)kCFProxyHostNameKey]);
	NSLog(@"port=%@", [settings objectForKey:(NSString *)kCFProxyPortNumberKey]);
	NSLog(@"type=%@", [settings objectForKey:(NSString *)kCFProxyTypeKey]);
	
	if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"]){
			//没有设置代理
		return NO;
	}else{
			//设置代理了
		return YES;
	}
}

+ (UIWindow *)mainWindow{
	return [UIApplication sharedApplication].delegate.window;
}

+ (CGFloat)safeAreaTop{
	if (@available(iOS 11.0, *)) {
		return [[UIApplication sharedApplication].delegate window].safeAreaInsets.top;
	}else{
		return 22;
	}
}

+ (CGFloat)safeAreaBottom{
	if (@available(iOS 11.0, *)) {
		return [[UIApplication sharedApplication].delegate window].safeAreaInsets.bottom;
	}else{
		return 0;
	}
}

+ (CGFloat)safeStatusBarBottom{
	return [[UIApplication sharedApplication] statusBarFrame].size.height;
}

+ (CGFloat)safeNavigationBottom{
	return self.safeStatusBarBottom + 44;
}

static float _zoomProportion;
+ (float)zoomProportion{
	_zoomProportion = 375.f / self.mainWindow.frame.size.width;
	return _zoomProportion;
}

@end
